<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>{$Think.lang.file_manage}</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    {include file="public/file_css"}
    <style>
        .file_icon{width:30px;height:30px;}
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div id="toolDel" style="display:none;">
            <a class="layui-btn layui-btn-sm" href="{:url('file/')}">{$Think.lang.root_director}</a>
            <a class="layui-btn layui-btn-sm" lay-event="create" action="{:url('file/create')}?path={$path}">{$Think.lang.create_file}</a>
            <a class="layui-btn layui-btn-sm" lay-event="createDir" action="{:url('file/createdir')}?path={$path}">{$Think.lang.create_director}</a>
            <button class="layui-btn layui-btn-sm layui-btn-danger" action="{:url('file/delAll')}" lay-event="del">
                {$Think.lang.batchDel} </button>
            <span style="margin-left:60px;">
                <td colspan="3">
                    {if $path}
                    <a href="{:url('file/index')}?path={$url|urlencode}">
                        <img src="/static/img/dir2.gif">
                        {$Think.lang.return_page}
                    </a>
                    {/if}
                </td>
                <td colspan="4">
                    {$Think.lang.current_dir}：/{$path}
                </td>
            </span>
        </div>
        <table lay-filter="backup">
            <thead>
            <tr>
                <th lay-data="{field:'numbers',type:'checkbox', width:60}">-</th>
                <th lay-data="{field:'filename'}">{$Think.lang.file_name}</th>
                <th lay-data="{field:'size',width:100}">{$Think.lang.file_size}</th>
                <th lay-data="{field:'mtime',width:180}">{$Think.lang.update_time}</th>
                <th lay-data="{field:'option',fixed:'right',width:220}">{$Think.lang.operate}</th>
            </tr>
            </thead>
            <tbody>

            {volist name='list' id='vo'}
            <tr>
                <td> - </td>
                <td>
                    {if $vo.type == 'dir'}
                    <a alt="{$vo.filename}" href="{:url()}?path={$path?$path.'/'|urlencode:''}{$vo.filename}">
                        <img class="file_icon" src="/static/file_icon/{$vo.parts}.png">
                        {$vo.filename}
                    </a>
                    {else}
                    <a alt="{$vo.filename}" href="javascript:;">
                        <img class="file_icon" src="/static/file_icon/{$vo.parts}.png">
                        {$vo.filename}
                    </a>
                    {/if}
                </td>
                <td>
                    {if $vo.type == 'dir'}
                        <span style="cursor:pointer;" onclick="CountCompute(this,'{$path?$path.\'/\':\'\'}{$vo.filename}')">
                            {$vo.size}
                        </span>
                    {else}
                        {$vo.size}
                    {/if}
                </td>
                <td>{$vo.mtime}</td>
                <td>
                    {if $vo.mime_type == 'text'}
                    <button class="layui-btn layui-btn-xs" lay-event="edit">{$Think.lang.edit_file}</button>
                    {/if}
                    {if $vo.type == 'file'}
                    <button class="layui-btn layui-btn-xs" lay-event="remove">{$Think.lang.remove_file}</button>
                    {/if}
                    <button class="layui-btn layui-btn-xs" lay-event="rename">{$Think.lang.update_file_name}</button>
                    <button class="layui-btn-danger layui-btn layui-btn-xs" lay-event="del">{$Think.lang.del}</button>
                </td>
            </tr>
            {/volist}
            </tbody>
        </table>
    </div>
</div>
{include file="public/file_js"}
<script src="/hyy1.0.0/js/jquery.min.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table;
        //转换静态表格
        table.init('backup', {
            //height: 315 //设置高度
            limit: 100, //注意：请务必确保 limit 参数（默认：10）是与你服务端限定的数据条数一致
            toolbar:"#toolDel"
            //支持所有基础参数
        })
        table.on('toolbar(backup)',function (obj) {
            let checkStatus = table.checkStatus(obj.config.id); //idTest 即为基础参数 id 对应的值
            let data = checkStatus.data;
            let fieldName = $(this).attr('field');
            if(!fieldName){
                fieldName = 'filename';
            }
            let actionUrl = $(this).attr('action');
            if(!actionUrl){
                actionUrl = 'Change';
            }
            let confirm = $(this).attr('title');
            if(!confirm){
                confirm = '{$Think.lang.collection_confirm}';
            }
            let arr = new Array();
            switch (obj.event) {
                case 'create':
                    ChangeData(actionUrl);
                    break;
                case 'createDir':
                    ChangeData(actionUrl);
                    break;
                case 'del':
                    for(let i = 0;i<data.length;i++){
                        let path = "{$path}";
                        let name = $(data[i][fieldName]).attr('alt');
                        arr.push(path+'/'+name); //ar_id 是数据表唯一id
                    }
                    layer.confirm(confirm, function(index){
                        Del(actionUrl,arr);  //执行批量删除
                        layer.close(index);
                    });
                    break;
            }
        });
        table.on('tool(backup)',function (obj) {
            let data = obj.data;
            let event = obj.event;
            let name = $(data['filename']).attr('alt');
            switch (event) {
                case 'rename':
                    ChangeData('{:url("file/rename")}?path={$path}/'+name);
                    break;
                case 'edit':
                    ChangeData('{:url("file/edit")}?path={$path}/'+name);
                    break;
                case 'remove':
                    ChangeData('{:url("file/remove")}?path={$path}/'+name);
                    break;
                case 'del':
                    layer.confirm('{$Think.lang.collection_confirm}', function(index){
                        Del('{:url("file/delAll")}?path={$path}/'+name);
                        obj.del();
                        layer.close(index);
                    });
                    break;
            }
        })
        function Del(url,obj){
            let index = layer.load(1,{shade: 0.3});
            $.ajax({
                url:url,
                type:"POST",
                data:{path:obj},
                success:function(result){
                    //权限不足
                    let ob = typeof result;
                    if(ob == 'object'){
                        if(ob.code == 0){
                            layer.msg(ob.msg,{icon:5,time:3000});
                        }
                    }
                    let obj = JSON.parse(result);
                    if(obj.code==200){
                        layer.msg(obj.msg,{icon:6,time:3000});
                        layer.close(index);
                        //移除选中行
                        $(".layui-form-checked").not('.header').parents('tr').remove();
                    }
                }
            });
        }
        function ChangeData(url){
            layer.open({
                type:2,
                fix: false, //不固定
                maxmin: true,
                shadeClose: true,
                shade:0.4,
                area:[$(window).width()*0.9+'px',$(window).height() - 50+'px'],
                content:url
            });
        }
    })
    //计算文件夹大小========================
    function CountCompute(obj,path=''){
        let th = obj;
        let val = $.trim($(th).text());
        if(val == '计算'){
            let index = layer.load(1,{shade:0.3});
            $.ajax({
                url:"{:url('countSize')}",
                type:"POST",
                data:{key:path},
                success:function(result){
                    //权限不足
                    let ob = typeof result;
                    if(ob == 'object'){
                        if(data.code == 0){
                            layer.msg(data.msg,{icon:5,time:3000});
                        }
                    }
                    let obj = JSON.parse(result);
                    if(obj.code==200){
                        $(th).html(obj.size);
                        layer.close(index);
                    }
                }
            });
        }
    }
</script>
</body>
</html>